import _ from 'lodash'
export default {
  data() {
    return {
      $_globalSideBar: null
    }
  },
  mounted() {
    this.$once('hook:beforeDestroy', () => {
      if (this.chart__obj && Object.getOwnPropertyNames(this.chart__obj).length > 0) {
        Object.keys(this.chart__obj).forEach((v) => {
          window.removeEventListener('resize', this.$_resizeChart)
          this.chart__obj[v].chart && this.chart__obj[v].chart.dispose()
          this.chart__obj[v] = null
        })
      }
    })
    this.$_chartEvent()
  },
  methods: {
    $_chartEvent() {
      this.$_globalSideBar = document.querySelector('#global-sidebar')
      if (typeof this.$_resizeChart === 'function') {
        window.addEventListener('resize', _.debounce(this.$_resizeChart, 150))
        this.$_globalSideBar.addEventListener('transitionend', _.debounce(this.$_resizeChart, 150))
      }
    },
    $_resizeChart() {
      if (this.chart__obj && Object.getOwnPropertyNames(this.chart__obj).length > 0) {
        Object.keys(this.chart__obj).forEach(v => {
          if (this.chart__obj[v] && this.chart__obj[v].resize) {
            this.chart__obj[v].resize()
          }
        })
      }
    }
  }
}
